package cn.ronghuanet.org.controller;

import cn.ronghuanet.base.annotation.RonghuaPermission;
import cn.ronghuanet.base.query.BaseQuery;
import cn.ronghuanet.base.util.AjaxResult;
import cn.ronghuanet.base.util.PageInfo;
import cn.ronghuanet.org.domain.Department;
import cn.ronghuanet.org.query.DepartmentQuery;
import cn.ronghuanet.org.service.IDepartmentService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.management.Query;
import java.util.Date;
import java.util.List;
import java.util.Objects;

@RestController
@RequestMapping("/department")
@Api(value = "部门管理",description = "部门管理")
@RonghuaPermission(name = "部门管理")
public class DepartmentController {
    @Autowired
    private IDepartmentService service;
    // 增 / 改

    /**
     * post请求 和 put请求 如果接受的参数是对象的时候 必要要使用 注解 @RequestBody
     * 才能够获取到值
     * @param department
     * @return
     */
    @ApiOperation(value = "该方法是用于新增或修改部门")
    @PutMapping
    @RonghuaPermission(name = "部门新增/修改")
    //以后 只要看到400 的状态码 那么就说明 你的数据格式不是后台所期望的 参数类型错误
    public AjaxResult insertOrUpdate(@RequestBody Department department){
        try {
            if (Objects.isNull(department.getId())){
                service.insert(department);
            }else{
                service.update(department);
            }
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }
    // 删
    @RonghuaPermission(name = "部门删除")
    @ApiOperation(value = "该方法是用于删除部门")
    @DeleteMapping("/{id}")
    public AjaxResult delete(@PathVariable("id")@ApiParam(name = "部门的id",required = true) Long id){
        try {
            service.delete(id);
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

    // 查
    @RonghuaPermission(name = "部门查询-根据id")
    @ApiOperation(value = "该方法是用于通过id查询部门")
    @GetMapping("/{id}")
    public AjaxResult selectById(@PathVariable("id")Long id){
        try {
            return  AjaxResult.success(service.queryById(id));
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }
    @RonghuaPermission(name = "查询全部部门")
    @ApiOperation(value = "该方法是用于查询所有部门")
    @GetMapping
    public AjaxResult queryAll(){
        try {
            return  AjaxResult.success(service.queryAll());
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }


    @RonghuaPermission(name = "部门分页查询")
    @PostMapping
    public AjaxResult queryPage(@RequestBody BaseQuery departmentQuery){
        try {
            PageInfo<Department> pageInfo = service.queryPage(departmentQuery);
            return  AjaxResult.success(pageInfo);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }


    @RonghuaPermission(name = "部门批量删除")
    @PatchMapping
    public AjaxResult batchDelete(@RequestBody List<Long> ids){
        try {
            service.batchDelete(ids);
            return  AjaxResult.success();
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }


    @GetMapping("/parent")
    //以后 只要看到400 的状态码 那么就说明 你的数据格式不是后台所期望的 参数类型错误
    public AjaxResult getParent(){
        try {
            List<Department> parents = service.getParent();
            return  AjaxResult.success(parents);
        } catch (Exception e) {
            e.printStackTrace();
            return  AjaxResult.error();
        }
    }

}
